﻿@model Matrix.MaKaYunShang.Model.ShopCommodity

@{
    ViewBag.Title = "编辑商品";
    ViewBag.PathToBtn = "/ShopCommodity/List";
    ViewBag.TextBtn = "返回至列表";
    Layout = "~/Views/Shared/_Layout.cshtml";
    var isLanMaSold = Model.IsLanmaSold;
}

<div class="row">
    <div class="col-lg-12">
        <div class="panel panel-default">
            <div class="panel-heading">
                商品信息
            </div>
            <div class="panel-body">
                <div class="row">
                    @using (Html.BeginForm("Update", "ShopCommodity", FormMethod.Post, new { id = "frmUpdate", enctype = "multipart/form-data" }))
                    {
                        @Html.AntiForgeryToken()
                        @Html.ValidationSummary(true)

                        <div class="col-lg-12">
                            <fieldset>
                                @Html.HiddenFor(model => model.Id)
                                @Html.HiddenFor(model => model.ShopId)
                                @Html.HiddenFor(model => model.CommodityLibId)
                                @Html.HiddenFor(model => model.AddTime)
                                @Html.HiddenFor(model => model.IsDelete)

                                <div class="form-group col-md-6">
                                    <label>卖价</label>
                                    <div class="input-group">
                                        @if (Model.IsShopCanModifyPrice)
                                        {
                                            @Html.TextBoxFor(model => model.SoldPrice, new { @class = "form-control", placeholder = "卖价" })
                                        }
                                        else
                                        {
                                            @Html.TextBoxFor(model => model.SoldPrice, new { @class = "form-control", placeholder = "卖价", disabled = "disabled" })
                                        }
                                        <span class="input-group-addon red">必填</span>
                                    </div>
                                </div>
                                <div class="form-group col-md-6">
                                    <label>数量</label>
                                    <div class="input-group">
                                        @Html.TextBoxFor(model => model.Quantity, new { @class = "form-control", placeholder = "数量" })
                                        <span class="input-group-addon red">必填</span>
                                    </div>
                                </div>
                                <div class="form-group col-md-6">
                                    <label>销量</label>
                                    <div class="input-group">
                                        @Html.TextBoxFor(model => model.SalesVolume, new { @class = "form-control", placeholder = "销量", @readonly = "readonly" })
                                        <span class="input-group-addon"></span>
                                    </div>
                                </div>
                                <div class="form-group col-md-6">
                                    <label>显示顺序</label>
                                    <div class="input-group">
                                        @{
                                            if (isLanMaSold)
                                            {
                                                @Html.TextBoxFor(model => model.DisplaySort, new { @class = "form-control", placeholder = "显示顺序", @readonly = "readonly" })
                                            }
                                            else
                                            {
                                                @Html.TextBoxFor(model => model.DisplaySort, new { @class = "form-control", placeholder = "显示顺序" })
                                            }
                                        }
                                        <span class="input-group-addon"></span>
                                    </div>
                                </div>
                                <div class="form-group col-md-6">
                                    <label>销售情况</label>
                                    <div class="input-group">
                                        <br />
                                        <label>
                                            @Html.RadioButtonFor(model => model.SellingStatus, "true") 上架
                                        </label>
                                        &nbsp;&nbsp;
                                        <label>
                                            @Html.RadioButtonFor(model => model.SellingStatus, "false") 下架
                                        </label>
                                    </div>
                                </div>
                                <input type="hidden" id="CeilingPrice" value="@Model.CeilingPrice" />
                                <input type="hidden" id="LowerPrice" value="@Model.LowerPrice" />
                                <div class="col-md-12">
                                    <button id="btnSave" type="button" class="btn btn-primary">确定并保存</button>
                                </div>
                            </fieldset>
                        </div>
                    }
                </div>
            </div>
        </div>
    </div>
</div>

@section javascript{
    <script type="text/javascript">
        $(function () {
            $("#frmUpdate").validate({
                rules: {
                    SoldPrice: { required: true, number: true },
                    Quantity: { required: true, number: true },
                    @{
                        if (isLanMaSold)
                        {
                            @Html.Raw("DisplaySort: { required: true, number: true }")
                        }
                        else
                        {
                            @Html.Raw("DisplaySort: { required: true, number: true, max:100000 }")
                        }
                    }
                },
                messages: {
                    SoldPrice: { required: "售价为必填项" },
                    Quantity: { required: "数量为必填项" },
                    DisplaySort: { required: "请填写排序序号" }
                }

            });

            $("#btnSave").click(function () {
                var soldPrice = parseFloat($("#SoldPrice").val());
                var ceilingPrice = parseFloat($("#CeilingPrice").val());
                var lowerPrice = parseFloat($("#LowerPrice").val());
                if (lowerPrice > 0) {
                    if (soldPrice < lowerPrice) {
                        return alertify.alert('零售价格不能小于下线价格:' + lowerPrice);
                    }
                }
                if (ceilingPrice > 0) {
                    if (soldPrice > ceilingPrice) {
                        return alertify.alert('零售价格不能大于上限价格:' + ceilingPrice);
                    }
                }
                if ($("#frmUpdate").valid()) {

                    $.ajax({
                        type: 'POST',
                        url: "/ShopCommodity/Update",
                        data: {
                            LowerPrice: $('#LowerPrice').val(),
                            Id: $('#Id').val(),
                            CeilingPrice: $("#CeilingPrice").val(),
                            Quantity: $("#Quantity").val(),
                            SoldPrice: $("#SoldPrice").val(),
                            CommodityLibId: $("#CommodityLibId").val(),
                            DisplaySort: $("#DisplaySort").val(),
                            IsLanmaSold: $("#IsLanmaSold").val(),
                            SellingStatus: $("#SellingStatus").val()
                },
                        success: function (info) {
                            if (info.Status) {
                                location.href = "/ShopCommodity/List";
                            } else {
                                alertify.error(info.Message);
                            }
                        }
                    });
                }
            });
        });
    </script>
}